package com.bida.job.cycle;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bida.domain.po.Orders;
import com.bida.service.IOrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

import static com.bida.constant.MessageConstant.ORDER_OVER_TIME_NO_PAY;
import static com.bida.constant.OrdersConstant.*;

/**
 * @author 23087
 *
 * 定时任务调度
 */
@Component
@Slf4j
public class OrdersCycleJob {

    @Autowired
    private IOrdersService ordersService;

    /**
     * 处理 15 min 内未支付的订单
     */
    @Scheduled(fixedDelay = 1000 * 60)
    public void handleNoPayMoney() {
        ordersService.lambdaUpdate()
                .set(Orders::getStatus, CANCELLED)
                .set(Orders::getCancelReason, ORDER_OVER_TIME_NO_PAY)
                .set(Orders::getCancelTime, LocalDateTime.now())
                .le(Orders::getOrderTime, LocalDateTime.now().minusMinutes(ORDERS_NO_PAY_TIMEOUT))
                .eq(Orders::getPayStatus, UN_PAID)
                .eq(Orders::getStatus, PENDING_PAYMENT)
                .update();
        log.info("{}，处理15分钟未支付的订单~", LocalDateTime.now());
    }

    /**
     * 处理 15 min 内未支付的订单
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void handleNoComplete() {
        ordersService.lambdaUpdate()
                .set(Orders::getStatus, COMPLETED)
                .set(Orders::getDeliveryTime, LocalDateTime.now())
                .eq(Orders::getStatus, DELIVERY_IN_PROGRESS)
                .update();
        log.info("{},打烊处理为完成派送的订单~",LocalDateTime.now());
    }
}
